#include "Admin_Func.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Person.h"
#include "Student_Func.h"
#include "Teacher_Func.h"
//管理员管理自己的函数

//登录验证
pAdmin_t Admin_Login_check(pAdmin_t phead)
{
    if(phead->next==phead)
    {
        printf("无管理员信息");
        return NULL;
    }
    pAdmin_t plink=phead->next;

    char account[20];
    char password[20];
    printf("请输入账号:");
    scanf("%s",account);
    while(getchar()!='\n');
    printf("请输入密码:");
    scanf("%s",password);
    while(getchar()!='\n');
    printf("\n");

    while(plink!=phead)
    {
        if(strcmp(plink->account,account)==0&&strcmp(plink->password,password)==0)
        {
            printf("登录成功\n");
            wait_for_any_key();
            return plink;
        }
        plink=plink->next;
    }
    printf("登录失败\n");
    wait_for_any_key();
    return NULL;

}


//有空节点的链表
//头节点不存放数据
pAdmin_t Admin_Init() {

    pAdmin_t phead = (pAdmin_t)malloc(sizeof(Admin_t));
    if (phead == NULL)
    {
        perror("malloc Admin fail");
        return phead;
    }
    memset(phead,0,sizeof(Admin_t));
    phead->next = phead;
    phead->prev = phead;
    return phead;
}

//创建节点
pAdmin_t Create_Admin_Node(char *account,char *password,char *name){

    pAdmin_t pnew=(pAdmin_t)malloc(sizeof(Admin_t));
    if(pnew==NULL)
    {
        perror("malloc a new node fail");
        return pnew;
    }
    strcpy(pnew->account,account);
    strcpy(pnew->password,password);
    strcpy(pnew->name,name);
    pnew->next=NULL;
    pnew->prev=NULL;
    return pnew;
}

//从键盘创建学生节点(学生只有创建账号密码和个人信息的权限,其它都初始化为0)
pAdmin_t Create_New_Admin_Scanf(void){

    pAdmin_t pnew=(pAdmin_t)malloc(sizeof(Admin_t));
    if(pnew==NULL)
    {
        perror("malloc a new node fail");
        return pnew;
    }
    //已经将所有成员初始化为0
    memset(pnew,0,sizeof(Admin_t));
    char account[50];
    char password[50];
    char name[20];
    printf("请输入账号：");
    scanf("%s",account);
    while(getchar()!='\n');
    printf("请输入密码：");
    scanf("%s",password);
    while(getchar()!='\n');
    printf("请输入姓名：");
    scanf("%s",name);
    while(getchar()!='\n');
    strcpy(pnew->account,account);
    strcpy(pnew->password,password);
    strcpy(pnew->name,name);
    pnew->next=NULL;
    pnew->prev=NULL;
    return pnew;
}


//头插法
void  Admin_Head_Insert(pAdmin_t phead, pAdmin_t node)
{   
   if(node == NULL)
   {
    perror("Head insert a node fail");
    return;
   }
   pAdmin_t plink=phead;
   //判断是否首节点
   if (phead->next == phead&&phead->prev==phead)
    {
        plink->prev = node;
        plink->next = node;
        node->prev = phead;
        node->next = phead;
        printf("注册成功\n");
        printf("个人`信息如下:\n");
        Print_One_Admin(node);
        return;
    }
   //头插
    node->next = phead->next;
    node->prev = phead;
    phead->next->prev=node;
    phead->next = node;
    printf("注册成功\n");
    printf("个人`信息如下:\n");
    Print_One_Admin(node);
}


//遍历链表
void Print_Admin_List(pAdmin_t phead)
{
    pAdmin_t plink=phead->next;
    if(plink==phead)
    {
        perror("None Link List");
        return;
    }
    while(plink!=phead)
    {
        printf("账号:%s,密码:%s,姓名:%s\n",plink->account,plink->password,plink->name);
        plink=plink->next;
    }
    printf("\n");

}

void Print_One_Admin(pAdmin_t plink)
{
    printf("账号:%s,密码:%s,姓名:%s\n",plink->account,plink->password,plink->name);
}


// 释放链表
void Release_Admin_List(pAdmin_t phead)
{
    pAdmin_t plink = phead->next;
    pAdmin_t ptemp = NULL;

   //不只有头结点的情况
    while (plink != phead)
    {
        ptemp = plink->next;
        free(plink);
        plink = ptemp;
    }
    free(phead);
    
    printf("链表已经全部释放\n");
}

//管理员添加学生
pStudent_t Admin_Create_New_Student_Scanf(void){

    pStudent_t pnew=(pStudent_t)malloc(sizeof(Student_t));
    if(pnew==NULL)
    {
        perror("malloc a new node fail");
        return pnew;
    }
    //已经将所有成员初始化为0
    memset(pnew,0,sizeof(Student_t));
    char account[50];
    char password[50];
    char name[20];
    int height;
    char sex;
    int score;
    printf("请输入账号：");
    scanf("%s",account);
    while(getchar()!='\n');
    printf("请输入密码：");
    scanf("%s",password);
    while(getchar()!='\n');
    printf("请输入姓名：");
    scanf("%s",name);
    while(getchar()!='\n');
    printf("请输入身高：");
    scanf("%d",&height);
    while(getchar()!='\n');
    printf("请输入性别: ");
    scanf("%c",&sex);
    while(getchar()!='\n');
    printf("请输入成绩: ");
    scanf("%d",&score);
    while(getchar()!='\n');
    strcpy(pnew->account,account);
    strcpy(pnew->password,password);
    strcpy(pnew->name,name);
    pnew->sex=sex;
    pnew->height=height;
    pnew->score=score;
    pnew->next=NULL;
    pnew->prev=NULL;
    return pnew;
}


//管理员管理学生函数/////////////////////////////////////////////////////////////////////////
//查找学生信息
void Admin_Find_Student(pStudent_t phead)
{
    char name[20];
    char account[50];
    char sex;
    int choice,height,score,status;
    if (phead->next == phead)
    {
        printf("无学生信息");
        return;
    }

    pStudent_t plink = phead->next;
     
    printf("选择需要查找学生的信息:1.账号 2.姓名  3.性别  4.身高 5.分数 6.状态 7.放弃查找\n");
    scanf("%d",&choice);
    while(getchar()!='\n');

    while(choice!=7)
    {
        switch(choice)
        {
            case 1:
                plink = phead->next;
                //没找到就是0，找到则为1
                int flag=0;
                printf("请输入要查找的学生账号:");
                scanf("%s",account);
                while(getchar()!='\n');
                while(plink!=phead)
                {
                    if(strcmp(plink->account,account)==0)
                    {
                        flag=1;
                        Print_One_Student(plink);
                    }
                    plink=plink->next;
                }
                if(flag==0)
                {
                    printf("未找到该学生账号\n");
                }
                break;
            case 2:
                plink = phead->next;
                //没找到就是0，找到则为1
                int flag1=0;
                printf("请输入要查找的学生姓名:");
                scanf("%s",name);
                while(getchar()!='\n');
                while(plink!=phead)
                {
                    if(strcmp(plink->name,name)==0)
                    {
                        flag1=1;
                        Print_One_Student(plink);
                    }
                    plink=plink->next;
                }
                if(flag1==0)
                {
                    printf("未找到该学生姓名\n");
                }
                break;
            case 3:
                plink = phead->next;
                //没找到就是0，找到则为1
                int flag2=0;
                printf("请输入要查找的学生性别:");
                scanf("%c",&sex);
                while(getchar()!='\n');
                while(plink!=phead)
                {
                    if(plink->sex==sex)
                    {
                        flag2=1; 
                        Print_One_Student(plink);
                    }
                    plink=plink->next;
                }
                if(flag2==0)
                {
                    printf("未找到该学生性别\n");
                }
                break;
            case 4:
                plink = phead->next;
                //没找到就是0，找到则为1
                int flag3=0;
                printf("请输入要查找的学生身高:");
                scanf("%d",&height);
                while(getchar()!='\n');
                while(plink!=phead)
                {
                    if(plink->height==height)
                    {
                        flag3=1; 
                        Print_One_Student(plink);
                    }
                    plink=plink->next;
                }
                if(flag3==0)
                {
                    printf("未找到该学生身高\n");
                }
                break;
            case 5:
                plink = phead->next;
                //没找到就是0，找到则为1
                int flag4=0;
                printf("请输入要查找的学生成绩:");
                scanf("%d",&score);
                while(getchar()!='\n');
                while(plink!=phead)
                {
                    if(plink->score==score)
                    {
                        flag4=1; 
                        Print_One_Student(plink);
                    }
                    plink=plink->next;
                }
                if(flag4==0)
                {
                    printf("未找到该学生成绩\n");
                }
                break;
            case 6:
                plink = phead->next;
                //没找到就是0，找到则为1
                int flag5=0;
                printf("请输入要查找的学生状态(0.在读 1.休学 2.退学):");
                scanf("%d",&status);
                while(getchar()!='\n');
                while(plink!=phead)
                {
                    if(plink->status==status)
                    {
                        flag5=1; 
                        Print_One_Student(plink);
                    }
                    plink=plink->next;
                }
                if(flag5==0)
                {
                    printf("未找到该学生状态\n");
                }
                break;
            case 7:
                printf("放弃查找\n");
                break;
            default:
                printf("输入错误，重新输入\n");
                break;
        }
        printf("选择需要查找学生的信息:1.账号 2.姓名  3.性别  4.身高 5.分数 6.状态 7.放弃查找\n");
        scanf("%d",&choice);
        while(getchar()!='\n');
        printf("\n");
    }
        


}

//管理员删除学生
void Admin_Delete_Student(pStudent_t phead)
{
    if(phead->next==phead)
    {
        printf("无学生信息");
        return;
    }

    pStudent_t plink=phead;
    pStudent_t ptemp=NULL;
    char account[20];
    printf("请输入要删除的账号：");
    scanf("%s",account);
    while(getchar()!='\n');
    printf("\n");
    //plink指向需要删除节点的前一个节点
    while(strcmp(plink->next->account,account)!=0)
    {
        if(plink->next==phead)
        {
            printf("无此学生\n");
            return;
        }

        plink=plink->next;
    }
    //ptemp保存要删除的节点的指针 
    ptemp=plink->next;
    ptemp->next->prev=plink;
    plink->next=ptemp->next;
    free(ptemp);
    ptemp=NULL;
}

//管理员修改学生信息
void Admin_Modify_Student(pStudent_t phead)
{
    char password[50];
    char account[50];
    char modifide_name[20];
    char sex;
    int choice,height,score,status;
    if (phead->next == phead)
    {
        printf("无学生信息");
        return;
    }

    printf("请输入要修改的学生账号:");
    scanf("%s",account);
    while(getchar()!='\n');
 
    pStudent_t plink = phead->next;

    while (plink != phead)
    {
        if (strcmp(plink->account,account)==0)
        {
            printf("选择需要修改的信息:1.密码 2.姓名  3.性别  4.身高 5.成绩 6.状态 7.放弃修改\n");
            scanf("%d",&choice);

            while(choice!=7)
            {
                switch(choice)
                {
                    case 1:
                        printf("请输入新的密码:");
                        scanf("%s",password);
                        while(getchar()!='\n');
                        strcpy(plink->password,password);
                        printf("密码修改成功\n");
                        break;
                    case 2:
                        printf("请输入新的学生姓名:");
                        scanf("%s",modifide_name);
                        while(getchar()!='\n');
                        strcpy(plink->name,modifide_name);
                        printf("姓名修改成功\n");
                        break;
                    case 3:
                        printf("请输入新性别:");
                        scanf("%s",&sex);
                        while(getchar()!='\n');
                        plink->sex=sex;
                        printf("性别修改成功\n");
                        break;
                    case 4:
                        printf("请输入新身高:");
                        scanf("%d",&height);
                        while(getchar()!='\n');
                        plink->height=height;
                        printf("身高修改成功\n");
                        break;
                    case 5:
                        printf("请输入新成绩:");
                        scanf("%d",&score);
                        while(getchar()!='\n');
                        plink->score=score;
                        printf("成绩修改成功\n");
                        break;
                    case 6:
                        printf("请输入新的状态(0.在读 1.休学 2.退学):");
                        scanf("%d",&status);
                        while(getchar()!='\n');
                        while(1)
                        {
                             if(status ==0 || status ==1 || status == 2)
                            {
                                break;
                            }
                            else
                            {
                                printf("请输入新的状态(0.在读 1.休学 2.退学):");
                                scanf("%d",&status);
                                while(getchar()!='\n');
                            }
                           
                        }
                        if(status == 0)
                            {plink->status = EMPLOYED;}
                        if(status == 1);
                            {plink->status = BREAK;}
                        if(status == 2)
                            {plink->status == DIMISSION;}

                        printf("状态修改成功\n");
                        break;
                    case 7:
                        return;
                        break;
                    default:
                        printf("输入错误，重新输入\n");
                        break;
                }
                printf("选择需要修改的信息:1.密码 2.姓名  3.性别  4.身高 5.成绩 6.状态 7.放弃修改\n");
                scanf("%d",&choice);
            }
            return;
        }
        plink = plink->next;
    }

    if (plink == phead)
    {
        printf("未找到该学生账号\n");
        return;
    }


}

//管理员打印学生信息
void Admin_Print_Student_List(pStudent_t phead)
{
    pStudent_t plink=phead->next;

    if(plink==phead)
    {
        perror("None Link List");
        return;
    }
    char *status[]={"在读","休学","退学"};
    while(plink!=phead)
    {
        printf("账号:%s,密码:%s,姓名:%s,性别:%c,身高:%d,成绩:%.2f,状态:%s\n",plink->account,plink->password,plink->name,plink->sex,plink->height,plink->score,status[plink->status]);
        plink=plink->next;
    }
    printf("\n");

}

////////////////////////////////////
//管理员管理老师函数
//打印老师信息
void Admin_Print_Teacher_List(pTeacher_t phead)
{
    pTeacher_t plink=phead->next;

    if(plink==phead)
    {
        perror("None Link List");
        return;
    }
    char *status[]={"在职","停职","离职"};
    while(plink!=phead)
    {
        printf("账号:%s,密码:%s,姓名:%s,性别:%c,年龄:%d,状态:%s\n",plink->account,plink->password,plink->name,plink->sex,plink->age,status[plink->status]);
        plink=plink->next;
    }
    printf("\n");

}

void Admin_Print_One_Teacher(pTeacher_t plink)
{
    char *status[]={"在职","停职","离职"};
    printf("账号:%s,密码:%s,姓名:%s,性别:%c,年龄:%d,状态:%s\n",plink->account,plink->password,plink->name,plink->sex,plink->age,status[plink->status]);
}

//删除老师
void Admin_Delete_Teacher(pTeacher_t phead)
{
    if(phead->next==phead)
    {
        printf("无老师信息");
        return;
    }

    pTeacher_t plink=phead;
    pTeacher_t ptemp=NULL;
    char account[20];
    printf("请输入要删除的账号：");
    scanf("%s",account);
    while(getchar()!='\n');
    printf("\n");
    //plink指向需要删除节点的前一个节点
    while(strcmp(plink->next->account,account)!=0)
    {
        if(plink->next==phead)
        {
            printf("无此老师\n");
            return;
        }

        plink=plink->next;
    }
    //ptemp保存要删除的节点的指针 
    ptemp=plink->next;
    ptemp->next->prev=plink;
    plink->next=ptemp->next;
    free(ptemp);
    printf("删除成功\n");
    ptemp=NULL;
}

//管理员修改老师信息
void Admin_Modify_Teacher(pTeacher_t phead)
{
    char password[50];
    char account[50];
    char modifide_name[20];
    char sex;
    int choice,age,status;
    if (phead->next == phead)
    {
        printf("无学生信息");
        return;
    }

    printf("请输入要修改的老师账号:");
    scanf("%s",account);
    while(getchar()!='\n');
 
    pTeacher_t plink = phead->next;

    while (plink != phead)
    {
        if (strcmp(plink->account,account)==0)
        {
            printf("选择需要修改的信息:1.密码 2.姓名  3.性别  4.年龄 5.状态 6.放弃修改\n");
            scanf("%d",&choice);

            while(choice!=6)
            {
                switch(choice)
                {
                    case 1:
                        printf("请输入新的密码:");
                        scanf("%s",password);
                        while(getchar()!='\n');
                        strcpy(plink->password,password);
                        printf("密码修改成功\n");
                        break;
                    case 2:
                        printf("请输入新的老师姓名:");
                        scanf("%s",modifide_name);
                        while(getchar()!='\n');
                        strcpy(plink->name,modifide_name);
                        printf("姓名修改成功\n");
                        break;
                    case 3:
                        printf("请输入新性别:");
                        scanf("%s",&sex);
                        while(getchar()!='\n');
                        plink->sex=sex;
                        printf("性别修改成功\n");
                        break;
                    case 4:
                        printf("请输入新年龄:");
                        scanf("%d",&age);
                        while(getchar()!='\n');
                        plink->age=age;
                        printf("身高修改成功\n");
                        break;
                    case 5:
                        printf("请输入新的状态(0.就职 1.停职 2.离职):");
                        scanf("%d",&status);
                        while(getchar()!='\n');
                        while(1)
                        {
                             if(status ==0 || status ==1 || status == 2)
                            {
                                break;
                            }
                            else
                            {
                                printf("输入错误，请输入新的状态(0.就职 1.停职 2.离职):");
                                scanf("%d",&status);
                                while(getchar()!='\n');
                            }
                           
                        }
                        if(status == 0)
                            {plink->status = EMPLOYED;}
                        if(status == 1);
                            {plink->status = BREAK;}
                        if(status == 2)
                            {plink->status == DIMISSION;}

                        printf("状态修改成功\n");
                        break;
                    case 6:
                        return;
                        break;
                    default:
                        printf("输入错误，重新输入\n");
                        break;
                }
                printf("选择需要修改的信息:1.密码 2.姓名  3.性别  4.年龄 5.状态 6.放弃修改\n");
                scanf("%d",&choice);
            }
            return;
        }
        plink = plink->next;
    }

    if (plink == phead)
    {
        printf("未找到该老师账号\n");
        return;
    }


}

//管理员查找老师信息
//查找老师信息
void Admin_Find_Teacher(pTeacher_t phead)
{
    char name[20];
    char account[50];
    char sex;
    int choice,age,status;
    if (phead->next == phead)
    {
        printf("无学生信息");
        return;
    }

    pTeacher_t plink = phead->next;
     
    printf("选择需要查找学生的信息:1.账号 2.姓名  3.性别  4.年龄 5.状态  6.放弃查找\n");
    scanf("%d",&choice);
    while(getchar()!='\n');

    while(choice!=6)
    {
        switch(choice)
        {
            case 1:
                plink = phead->next;
                //没找到就是0，找到则为1
                int flag=0;
                printf("请输入要查找的老师账号:");
                scanf("%s",account);
                while(getchar()!='\n');
                while(plink!=phead)
                {
                    if(strcmp(plink->account,account)==0)
                    {
                        flag=1;
                        Print_One_Teacher(plink);
                    }
                    plink=plink->next;
                }
                if(flag==0)
                {
                    printf("未找到该老师账号\n");
                }
                break;
            case 2:
                plink = phead->next;
                //没找到就是0，找到则为1
                int flag1=0;
                printf("请输入要查找的老师姓名:");
                scanf("%s",name);
                while(getchar()!='\n');
                while(plink!=phead)
                {
                    if(strcmp(plink->name,name)==0)
                    {
                        flag1=1;
                        Print_One_Teacher(plink);
                    }
                    plink=plink->next;
                }
                if(flag1==0)
                {
                    printf("未找到该老师姓名\n");
                }
                break;
            case 3:
                plink = phead->next;
                //没找到就是0，找到则为1
                int flag2=0;
                printf("请输入要查找的老师性别:");
                scanf("%c",&sex);
                while(getchar()!='\n');
                while(plink!=phead)
                {
                    if(plink->sex==sex)
                    {
                        flag2=1; 
                        Print_One_Teacher(plink);
                    }
                    plink=plink->next;
                }
                if(flag2==0)
                {
                    printf("未找到该老师性别\n");
                }
                break;
            case 4:
                plink = phead->next;
                //没找到就是0，找到则为1
                int flag3=0;
                printf("请输入要查找的老师年龄:");
                scanf("%d",&age);
                while(getchar()!='\n');
                while(plink!=phead)
                {
                    if(plink->age==age)
                    {
                        flag3=1; 
                        Print_One_Teacher(plink);
                    }
                    plink=plink->next;
                }
                if(flag3==0)
                {
                    printf("未找到该老师年龄\n");
                }
                break;
            case 5:
                plink = phead->next;
                //没找到就是0，找到则为1
                int flag4=0;
                printf("请输入要查找的老师状态(0.就职 1.停职 2.离职):");
                scanf("%d",&status);
                while(getchar()!='\n');
                while(plink!=phead)
                {
                    if(plink->status==status)
                    {
                        flag4=1; 
                        Print_One_Teacher(plink);
                    }
                    plink=plink->next;
                }
                if(flag4==0)
                {
                    printf("未找到该老师状态\n");
                }
                break;
            case 6:
                printf("放弃查找\n");
                break;
            default:
                printf("输入错误，重新输入\n");
                break;
        }
        printf("选择需要查找学生的信息:1.账号 2.姓名  3.性别  4.年龄 5.状态  6.放弃查找\n");
        scanf("%d",&choice);
        while(getchar()!='\n');
        printf("\n");
    }
        


}

